<?php 
//SectionSearch.php
//Implemented 2/10/10 
//@author Michael Slaughter

require_once("overhead.php");
//require_once("Search.php");

class SectionSearch {
	var $professor; 
	var $courseNumber;
	var $term;
	var $department;
	var $year;
	var $crn;
	var $sections;

// Setters required based on input
	public function setProfessor($prof){
		$this->professor = $prof;
	}	
	public function setCourseNumber($cn){
		$this->courseNumber = $cn;
	}	
	public function setTerm($term){
		$this->term = $term;
	}			
	public function setDepartment($depart){
		$this->department = $depart;
	}	
	public function setYear($year){
		$this->year = $year;
	}	
	public function setCrn($crn){
		$this->crn = $crn;
	}
	public function clear(){//Sets all objects to nulls
		$this->year = null;
		$this->professor = null;
		$this->courseNumber = null;
		$this->term = null;
		$this->department = null;
		$this->sections = null;
		
	}
	
	public function query(){ //Sets the Section array of this instance to the results of this query 

		$queryString = "select Section.* from Section"; 
		if(isset($this->crn)){ //If the CRN is set then search for that particular section and add it to the sections array
			$queryString = $queryString.' where Section.ID = "'.$this->crn.'"';
			$result = mysql_query($queryString);
		}
		else{

			$this->sections = array();
			$q = array(); 
		
			if (isset($this->department)){
				$q[] =	"DepartmentID = '".$this->department."'";			
			}
			if (isset($this->professor)){
				$q[] = " ProfessorID = '".$this->professor->getID()."'";			
			}
			if (isset($this->courseNumber)){
				$q[] =  " CourseID = '".$this->courseNumber->getID()."'";					
			}
			if (isset($this->term)){
				$q[] =	" TermID = ".$this->term->getID()."'";	
			}		
			if (isset($this->year)){
				$q[] =	" Year = '".$this->year."'";	
			}
		
				if($q) {
				$queryString = "select Section.* from Section where "; 
				$queryString = $queryString.implode(' and ', $q); //Join all of the search terms if they appear in the if statement 
				}
		
				//var_dump($queryString);
		}	
		
		$result = mysql_query($queryString);
		
		if($result){
			while($row = mysql_fetch_assoc($result)){
				$sect = new Section(); 
				$sect->ID = $row['ID']; //corresponds to the Primary key in the Section table (also called ID) 
	//*** Changed to just set the ID from the query manually ***
				$sect->setCode($row['Code']); //coresponds to the Section code (for example, for "CS 1050A" the code is "A")
				
				//Create a new Professor Object and set the Section's Professsor to it
				$prof = new Professor();
	 			$prof->retrieveFromID($row['ProfessorID']);
				$sect->setProfessor($prof);

				//Create a new Term Object and set the section's Term to it
	 			$t=new Term();
	 			$t->retrieveFromID($row['TermID']);
				$sect->setTerm($t);

				$sect->setPercent_A($row['Percent_A']);
				$sect->setPercent_B($row['Percent_B']);
				$sect->setPercent_C($row['Percent_C']);
				$sect->setPercent_D($row['Percent_D']);
				$sect->setPercent_F($row['Percent_F']);
				$sect->setPercent_W($row['Percent_W']);
				$sect->setSize($row['Size']);
				$sect->setGPA($row['GPA']);

				//Create a new Course Object and set the Sections's Course to it
	 			$c = new Course();
	 			$c->retrieveFromID($row['CourseID']);
				$sect->setCourse($c);
				
				$this->sections[] = $sect;
			}
		
		}
	    
	}
	public function getAllResults(){
		return $this->sections; //Return the array of Sections gained off of a successful query
	}
	public function getResult($index){
		return $this->sections[$index]; //Return the Section object at the position index
	}
	public function sort($fieldName){
	/* Will implement at a later date */ 
	}
		
}
